﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp10
{
    public partial class Form2 : Form
    {
        private SELECT select;
        private bool isUpdate;
        public Action update;
        public Form2(SELECT se,bool isu)
        {
            InitializeComponent();
            select = se;
            isUpdate = isu;
            if (select == SELECT.Student)
            {
                label4.Show();
                label3.Show();
                textBox3.Show();
                textBox4.Show();
            }
            else if (select == SELECT.Class)
            {
                label4.Hide();
                label3.Show();
                textBox3.Show();
                textBox4.Hide();
            }
            else if (se == SELECT.School)
            {
                label4.Hide();
                label3.Hide();
                textBox3.Hide();
                textBox4.Hide();
            }

            if (isUpdate)
            {
                label5.Show();
                textBox5.Show();
            }else
            {
                label5.Hide();
                textBox5.Hide();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;"))
            {
                connection.Open();
                using (SQLiteTransaction transaction = connection.BeginTransaction())
                {
                    if (select == SELECT.Student)
                    {
                        Student st = new Student(int.Parse(textBox1.Text), textBox2.Text, int.Parse(textBox4.Text), int.Parse(textBox3.Text));
                        if (isUpdate)
                        { 
                            Form1.UpdateStudent(st, connection);
                        }
                        else
                        {
                            Form1.InsertStudent(connection, st);
                        }
                    }
                    else if (select == SELECT.Class)
                    {
                        Class c = new Class(int.Parse(textBox1.Text), textBox2.Text, int.Parse(textBox3.Text));
                        if (isUpdate)
                        {
                            Form1.UpdateClass(c, connection);
                        }
                        else
                        {
                            Form1.InsertClass(connection, c);
                        }
                        
                    }
                    else if (select == SELECT.School)
                    {
                        School sc = new School(int.Parse(textBox1.Text), textBox2.Text);
                        if (isUpdate)
                        {
                            Form1.UpdateSchool(sc, connection);
                        }
                        else
                        {
                            Form1.InsertSchool(connection, sc);
                        }
                        
                    }

                    transaction.Commit();
                }
                connection.Close();
            }
            update();
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
